package com.example.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.springboot.service.IUserService;
import com.example.springboot.entity.User;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小申
 * @since 2023-08-14
 */
@RestController
@RequestMapping("/user")
public class UserController {
    
    @Resource
    private IUserService userService;

    @Resource
    UserMapper userMapper;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody User user){
        return Result.success(userService.saveOrUpdate(user));
    }

    @PostMapping("/sign")
    public Result sign(@RequestBody User user){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",user.getAccount());
        User login=userService.getOne(queryWrapper);
        if(login!=null){
            return Result.success("用户名已存在");
        }
        user.setActivityLen(0);
        user.setCollectionLen(0);
        user.setFansLen(0);
        user.setFocosLen(0);
        user.setAvatar("http://kin.qinsy.ltd:9080/activity-img/IMG_0121_1694933980462.PNG");
        user.setNickname("新用户");
        user.setSex(true);
        userMapper.insert(user);
        return Result.success(user.getId());
    }

    @PostMapping("/login")
    // 新增或者更新
    public Result login(@RequestBody User user){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",user.getAccount());
        User login=userService.getOne(queryWrapper);
        if(login==null){
            return Result.success("用户名不存在");
        }
        if(login.getPassword().equals(user.getPassword())){
            return Result.success(login.getId());
        }
        return Result.success("密码错误");
    }

    //删除单条数据
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(userService.removeById(id));
    }

    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(userService.removeByIds(ids));
    }

    //获取全部数据
    @GetMapping
    public Result findAll(){
        return Result.success(userService.list());
    }

    //获取单条数据
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id){
        return Result.success(userService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(userService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
}

